package com.mindmap.ui;

import com.mindmap.model.MindMap;
import com.mindmap.model.Node;
import com.mindmap.util.FileManager;
import com.mindmap.util.LayoutManager;
import javax.swing.*;
import java.awt.*;
import java.io.File;

public class MainFrame extends JFrame {
    private MindMap mindMap;
    private DrawingPanel drawingPanel;
    private StructurePanel structurePanel;
    private ToolBarPanel toolBarPanel;

    public MainFrame() {
        initialize();
        setupComponents();
        newMindMap();
    }
    

    private void initialize() {
        setTitle("未命名思维导图-思维导图工具");
        setSize(1000, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void setupComponents() {
        drawingPanel = new DrawingPanel(this);
        structurePanel = new StructurePanel(this);
        toolBarPanel = new ToolBarPanel(this);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, drawingPanel, structurePanel);
        splitPane.setDividerLocation(700);

        add(toolBarPanel, BorderLayout.NORTH);
        add(splitPane, BorderLayout.CENTER);
    }
    public void newMindMap() {
        mindMap = new MindMap();
        String defaultTitle = "未命名思维导图";
        mindMap.setTitle(defaultTitle);
        setTitle(defaultTitle + " - 思维导图工具");

        // 根节点使用与导图相同的标题
        Node root = new Node(defaultTitle);
        mindMap.setRoot(root);
        updateUI();
    }

    public void openMindMap() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("思维导图文件 (*.dt)", "dt"));

        if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            mindMap = FileManager.load(file);
            if (mindMap != null) {
                updateUI();
            } else {
                JOptionPane.showMessageDialog(this, "打开文件失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void saveMindMap() {
        if (mindMap == null) return;

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("思维导图文件 (*.dt)", "dt"));

        if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            if (!file.getName().endsWith(".dt")) {
                file = new File(file.getPath() + ".dt");
            }
            FileManager.save(mindMap, file);
        }
    }

    public void exportToImage() {
        if (mindMap == null) return;

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("PNG 图片 (*.png)", "png"));
        fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG 图片 (*.jpg)", "jpg"));

        if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            String format = fileChooser.getFileFilter().getDescription().contains("PNG") ? "png" : "jpg";
            if (!file.getName().toLowerCase().endsWith("." + format)) {
                file = new File(file.getPath() + "." + format);
            }

            drawingPanel.exportToImage(file, format);
        }
    }

    public void addChildNode() {
        if (mindMap == null || mindMap.getSelectedNode() == null) {
            JOptionPane.showMessageDialog(this, "请先选择一个节点");
            return;
        }

        String name = JOptionPane.showInputDialog(this, "输入子节点名称:");
        if (name != null && !name.trim().isEmpty()) {
            Node newNode = new Node(name);
            mindMap.getSelectedNode().addChild(newNode);
            updateUI();
        }
    }

    public void addSiblingNode() {
        if (mindMap == null || mindMap.getSelectedNode() == null) {
            JOptionPane.showMessageDialog(this, "请先选择一个节点");
            return;
        }

        if (mindMap.getSelectedNode() == mindMap.getRoot()) {
            JOptionPane.showMessageDialog(this, "中心节点不能添加兄弟节点");
            return;
        }

        String name = JOptionPane.showInputDialog(this, "输入兄弟节点名称:");
        if (name != null && !name.trim().isEmpty()) {
            Node newNode = new Node(name);
            Node parent = mindMap.getSelectedNode().getParent();
            parent.addChild(newNode);
            updateUI();
        }
    }

    public void deleteNode() {
        if (mindMap == null || mindMap.getSelectedNode() == null) {
            JOptionPane.showMessageDialog(this, "请先选择一个节点");
            return;
        }

        if (mindMap.getSelectedNode() == mindMap.getRoot()) {
            JOptionPane.showMessageDialog(this, "不能删除中心节点");
            return;
        }

        int confirm = JOptionPane.showConfirmDialog(this,
                "确定要删除这个节点及其所有子节点吗?", "确认删除",
                JOptionPane.YES_NO_OPTION);

        if (confirm == JOptionPane.YES_OPTION) {
            Node parent = mindMap.getSelectedNode().getParent();
            parent.removeChild(mindMap.getSelectedNode());
            mindMap.setSelectedNode(null);
            updateUI();
        }
    }

    public void setAutoLayout() {
        if (mindMap != null) {
            LayoutManager.autoLayout(mindMap);
            updateUI();
        }
    }

    public void setLeftLayout() {
        if (mindMap != null) {
            LayoutManager.leftLayout(mindMap);
            updateUI();
        }
    }

    public void setRightLayout() {
        if (mindMap != null) {
            LayoutManager.rightLayout(mindMap);
            updateUI();
        }
    }

    public void updateUI() {
        drawingPanel.setMindMap(mindMap);
        structurePanel.updateStructure(mindMap);
        drawingPanel.repaint();
    }

    public MindMap getMindMap() {
        return mindMap;
    }

    // 添加修改思维导图标题的方法
    public void changeTitle() {
        if (mindMap == null) {
            JOptionPane.showMessageDialog(this, "请先创建或打开思维导图");
            return;
        }

        String currentTitle = mindMap.getTitle();
        // 获取用户输入的新标题
        String newTitle = (String) JOptionPane.showInputDialog(
                this,
                "请输入新标题:",
                "修改思维导图标题",
                JOptionPane.PLAIN_MESSAGE,
                null,
                null,
                currentTitle
        );

        if (newTitle != null && !newTitle.trim().isEmpty()) {
            // 更新思维导图标题
            mindMap.setTitle(newTitle);
            // 更新窗口标题
            setTitle(newTitle + " - 思维导图工具");
            // 更新根节点标题
            if (mindMap.getRoot() != null) {
                mindMap.getRoot().setText(newTitle);
            }
            // 刷新UI
            updateUI();
        }
    }
}
